package com.zengdada.authority.service.impl;

import com.zengdada.authority.mapper.AppDictionaryMapper;
import com.zengdada.authority.model.AppDictionary;
import com.zengdada.authority.service.AppDictionaryAuthorityService;
import com.zengdada.authority.service.EnhanceBaseService;
import com.zengdada.authority.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.List;
import java.util.Map;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2018/11/26 21:39
 * @Description:
 */
@Service
public class AppDictionaryAuthorityServiceImpl extends EnhanceBaseService<AppDictionary> implements AppDictionaryAuthorityService {

    @Value("${authority.application.id}")
    private String APPLICATION_ID;

    @Override
    public List<Map<String, Object>> recursiveQuery(List<String> startIds, boolean retainHead) {
        return CommonUtils.depthTransformHumpNew(((AppDictionaryMapper) mapper).recursiveQuery(startIds != null && startIds.size() > 0 ? startIds : null, retainHead, APPLICATION_ID));
    }

    @Override
    public List<Map<String, Object>> recursiveQueryTree(List<String> startIds, boolean retainHead, String childrenName) {
        return CommonUtils.generateTreeMapCurrency(recursiveQuery(startIds, retainHead), "id", "parentId", childrenName);
    }

    @Override
    public List<AppDictionary> getDictionaryListByParentId(String parentId) {
        return mapper.selectByExample(new Example.Builder(AppDictionary.class).where(WeekendSqls.<AppDictionary>custom().andEqualTo(AppDictionary::getParentId, parentId).andEqualTo(AppDictionary::getApplicationId, APPLICATION_ID)).build());
    }

    @Override
    public AppDictionary getDictionaryByParentIdAndValue(String parentId, String value) {
        List<AppDictionary> dictionaries = mapper.selectByExample(new Example.Builder(AppDictionary.class).where(WeekendSqls.<AppDictionary>custom().andEqualTo(AppDictionary::getParentId, parentId).andEqualTo(AppDictionary::getValue, value).andEqualTo(AppDictionary::getApplicationId, APPLICATION_ID)).build());
        if (dictionaries.size() == 0) return null;
        return dictionaries.get(0);
    }


}
